//
//  HomeViewController.swift
//  yaokongqi
//
//  Created by HappyMilk 02 on 2017/9/26.
//  Copyright © 2017年 nike. All rights reserved.
//

import UIKit

class HomeViewController: UIViewController ,UITableViewDelegate ,UITableViewDataSource {
    
    
    var TVView: UIView!
    
    @IBOutlet weak var view_list: UIView!
    @IBOutlet weak var view_zhe: UIView!
    @IBOutlet weak var tabelview: UITableView!
    
    var isShow: Bool = false
    
    var back: UIView!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        changeRemoteControl()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(recoveryListView))
        view_zhe.addGestureRecognizer(tap)
        
        let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TwoViewController")
        self.addChildViewController(vc)
        let vc1 = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FourViewController")
        self.addChildViewController(vc1)
        
        NotificationCenter.default.addObserver(self, selector: #selector(showHintView), name: NSNotification.Name(rawValue: "show"), object: nil)

        
    }
    
    func appInfoLoad() {
        
//        if is_pass() {
//            let hhh: CGFloat = is_ipad() ? 139 : 99
//            let banner = GADBannerView.init(adSize: kGADAdSizeSmartBannerPortrait, origin: CGPoint.init(x: 0, y: ScreenHeight-hhh))
//            banner.adUnitID = "ca-app-pub-4634576436183649/5461632056"
//            banner.rootViewController = self.tabBarController
//            self.tabBarController?.view.addSubview(banner)
//            banner.load(GADRequest())
//        }
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        recoveryListView()
    }
    
    func changeRemoteControl() {
//        let first = UserDefaults.standard.object(forKey: "first") as? String
//        if (first == "1") {
//            TVView.removeFromSuperview()
//        }
        
        
        let type = UserDefaults.standard.object(forKey: "type") as? String
        if type == "1" {
            TVView = UIView.init(frame: CGRect.init(x: 0, y: 64, width: ScreenWidth, height: ScreenHeight-49-64))
            view.addSubview(TVView)
            view.insertSubview(TVView, belowSubview: view_zhe)
            let vc = self.childViewControllers.first!
            vc.view.frame = CGRect.init(x: 0, y: 0, width: ScreenWidth, height: TVView.frame.size.height)
            TVView.addSubview(vc.view)
        }
        if type == "2" {
            TVView = UIView.init(frame: CGRect.init(x: 0, y: 64, width: ScreenWidth, height: ScreenHeight-49-64))
            view.addSubview(TVView)
            view.insertSubview(TVView, belowSubview: view_zhe)
            let vc1 = self.childViewControllers.last!
            vc1.view.frame = CGRect.init(x: 0, y: 0, width: ScreenWidth, height: TVView.frame.size.height)
            TVView.addSubview(vc1.view)
        }
    }
    
    
    
    @IBAction func addRemoteControl(_ sender: UIButton) {
        
        AdLibrary.stander().showAd()

        
        let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AddViewController")
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
    func recoveryListView() {
        
        UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseInOut, animations: {
            
            self.view_zhe.alpha = 0.0
            self.view_list.frame = CGRect.init(x: -270, y: 0, width: self.view_list.frame.size.width, height: self.view_list.frame.size.height)
        }, completion: { (finlish) in
            self.view_zhe.isHidden = true
        })
        isShow = false
    }
    
    @IBAction func showListView(_ sender: UIButton) {
        
        AdLibrary.stander().showAd()
        
        
        UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseInOut, animations: {
            
            self.view_zhe.isHidden = false
            self.view_zhe.alpha = 0.45
            self.view_list.frame = CGRect.init(x: 0, y: 0, width: self.view_list.frame.size.width, height: self.view_list.frame.size.height)
        }, completion: { (finlish) in
            
        })
        isShow = true
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = Bundle.main.loadNibNamed("ListViewCell", owner: nil, options: nil)?.last as! ListViewCell
        cell.imgv.image = UIImage.init(named: "h-c\(indexPath.row+1)")
        if indexPath.row == 0 {
            cell.lab_title.text = is_chinese() ? "电视" : "TV"
        }
        if indexPath.row == 1 {
            cell.lab_title.text = is_chinese() ? "空调" : "Air conditioner"
        }
        if indexPath.row == 2 {
            cell.lab_title.text = is_chinese() ? "查找控制器" : "Scearch Control"
        }
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if indexPath.row == 0 {
            UserDefaults.standard.set("1", forKey: "type")
            UserDefaults.standard.set("1", forKey: "first")
            changeRemoteControl()
            recoveryListView()
        }
        if indexPath.row == 1 {
            UserDefaults.standard.set("2", forKey: "type")
            UserDefaults.standard.set("1", forKey: "first")
            changeRemoteControl()
            recoveryListView()
        }
        if indexPath.row == 2 {
            let open = UserDefaults.standard.object(forKey: "open") as? String
            if open == "1" {
                AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
            }
            showHintView()
        }
    }
    
    func showHintView() {
        let window = UIApplication.shared.keyWindow
        
        back = UIView.init(frame: self.view.frame)
        back.backgroundColor = UIColor.clear
        back.alpha = 0.0
        window?.addSubview(back)
        
        let zhe = UIView.init(frame: back.frame)
        zhe.backgroundColor = UIColor.black
        zhe.alpha = 0.45
        back.addSubview(zhe)
        
        let hint = Bundle.main.loadNibNamed("HintViewCell", owner: nil, options: nil)?.last as! HintViewCell
        hint.frame = CGRect.init(x: 0, y: 0, width: 340, height: 290)
        hint.center = back.center
        hint.backgroundColor = UIColor.white
        hint.layer.masksToBounds = true
        hint.layer.cornerRadius = 10
        back.addSubview(hint)
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(hiddenVack))
        zhe.addGestureRecognizer(tap)
        
        UIView.animate(withDuration: 0.4) { 
            self.back.alpha = 1.0
        }
    }
    
    func hiddenVack() {
        
        UIView.animate(withDuration: 0.4, animations: { 
            self.back.alpha = 0.0
        }) { (finlish) in
            self.back.removeFromSuperview()
        }
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
